package com.cainli.image;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class ChannelDrawable extends ProxyDrawable
{
//    private statCanvas emptyCanvas = new Canvas();

    /**
     * 混合模式
     */
    private static PorterDuffXfermode sXferMode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    
    private Paint mPaint;
    
//    private Shape mChannelShape;
    
    private Bitmap mChannelBitmap;
    
    private NinePatch mChannelNinePath;
    
    public static final int CHANNEL_TYPE_NULL = -1;
    public static final int CHANNEL_TYPE_BITMAP = 0;
//    public static final int CHANNEL_TYPE_SHAPE = 1;
    public static final int CHANNEL_NINE_PATH = 1;
    
    private int mChannelType = -1;
    
    
    /**
     * 使用Bitmap作为通道, 该bitmap会根据drawable大小缩放后进行通道裁减
     * @param drawable
     * @param channelBitmap
     */
    public ChannelDrawable(Drawable drawable)
    {
        super(drawable);
        mPaint = new Paint();
        mPaint.setXfermode(sXferMode);
        mPaint.setColor(Color.RED);
    }
    
    /**
     * 设置通道图片
     * @param bitmap
     */
    public void setChannel(Bitmap bm)
    {
        if(bm == null)
        {
            throw new IllegalArgumentException("null argument");
        }
        byte[] np = bm.getNinePatchChunk();
        // 不是9PNG
        if (np == null || !NinePatch.isNinePatchChunk(np)) {
            np = null;
            mChannelType = CHANNEL_TYPE_BITMAP;
            mChannelBitmap = bm;
//            mChannelShape = null;
            mChannelNinePath = null;
        }
        // 9PNG
        else
        {
            mChannelType = CHANNEL_NINE_PATH;
            mChannelBitmap = null;
//            mChannelShape = null;
            mChannelNinePath = new NinePatch(bm, bm.getNinePatchChunk(), "XML 9-patch");
        }
    }
    
//    public void setChannel(Shape shape)
//    {
//        mChannelType = CHANNEL_TYPE_SHAPE;
//        mChannelBitmap = null;
//        mChannelShape = shape;
//        mChannelNinePath = null;
//        updateShape();
//    }
    
//    private void updateShape()
//    {
//        if (mChannelShape != null)
//        {
//            final Rect r = getBounds();
//            final int w = r.width();
//            final int h = r.height();
//
//            mChannelShape.resize(w, h);
//            invalidateSelf();
//        }
//    }
//    @Override
//    protected void onBoundsChange(Rect bounds)
//    {
//        super.onBoundsChange(bounds);
//        updateShape();
//    }

    @Override
    public void draw(Canvas canvas)
    {
        
        if(mChannelType == CHANNEL_TYPE_NULL)
        {
            mCurrDrawable.draw(canvas);
            return;
        }
        Rect bounds = getBounds();
        // 存储
        int restoreCount = canvas.saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, null, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
        // 画原图
        mCurrDrawable.draw(canvas);
        // 画通道, 并进行裁减
        drawChannel(canvas,bounds);        
        canvas.restoreToCount(restoreCount);
    }
    
    private void drawChannel(Canvas canvas, Rect bounds)
    {
//        Xfermode oldXferMode = mPaint.getXfermode();
//        mPaint.setXfermode(sXferMode);
        switch (mChannelType)
        {
            case CHANNEL_TYPE_BITMAP:
                canvas.drawBitmap(mChannelBitmap, null, bounds, mPaint);
                break;
//            case CHANNEL_TYPE_SHAPE:
////                mChannelShape.resize(bounds.width(), bounds.height());
//                int count = canvas.save();
//                canvas.translate(bounds.left, bounds.top);
//                mChannelShape.draw(canvas, mPaint);
//                canvas.restoreToCount(count);
//                break;
                
            case CHANNEL_NINE_PATH:
                mChannelNinePath.draw(canvas, bounds, mPaint);
                break;
            default:
                break;
        }
//        mPaint.setXfermode(xfermode)
    }

}
